<?php

namespace app_admin\models;

use app_admin\interfaces\OsInterface;
use Yii;
use yii\base\Model;

use common\models\CM_SysAuth;

class  OsModel extends Model implements OsInterface
{

    public function actionList()
    {
        yii::$app->debug->stime("model");
        $list = CM_SysAuth::get_OsList();
        $nlist = array();
        foreach ($list as $item) {
            if ($item['parent_id'] == '0') {
                $citems = array();
                foreach ($list as $citem) {
                    if ($citem['parent_id'] == $item['id'])
                        $citems[] = $citem;
                }

                $nlist[] = array("name" => $item['name'], "items" => $citems);
            }
        }

        yii::$app->debug->etime("model");
        $data['list'] = $nlist;
        return $data;
    }

    public function actionEdit()
    {
        $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
        $info = CM_SysAuth::get_os($id);

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $data = array(
                "parent_id" => $_POST['parent_id'],
                "name" => $_POST['title'],
                "type" => $_POST['type'],
                //"query"=>$_POST['title'],
                "remark" => $_POST['remark']
            );
            if (isset($info['id'])) {
                CM_SysAuth::edit_Os($id, $data);
            }else
                CM_SysAuth::add_Os($data);

            echo "ok";
            exit;
        }

        $data['info'] = $info;
        $data['cates'] = CM_SysAuth::get_ParentOsList();
        return $data;
    }
}
